//
//  JkmNewfeatureViewController.swift
//  SinaWb
//
//  Created by Jiang on 16/5/15.
//  Copyright © 2016年 Jiang. All rights reserved.
//

import UIKit

private let reuseIdentifier = "Cell"
private let  itemsCount = 4
class JkmNewfeatureViewController: UICollectionViewController {
    @objc func clickButton(){

        NSNotificationCenter.defaultCenter().postNotificationName("JkmAppDelegate", object: nil, userInfo: nil)
    }
    
    init(){
        //设置布局
        let layer = UICollectionViewFlowLayout()
        layer.minimumInteritemSpacing = 0
        layer.minimumLineSpacing = 0
        layer.scrollDirection = .Horizontal
        layer.itemSize = UIScreen.mainScreen().bounds.size
        
        super.init(collectionViewLayout: layer)
        
        prepareLayout()
    }
    func prepareLayout(){
        
        collectionView?.pagingEnabled = true
        collectionView?.showsVerticalScrollIndicator = false
        collectionView?.showsHorizontalScrollIndicator = false
        collectionView?.bounces = false
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
    
    override func viewDidLoad() {
        super.viewDidLoad()
        self.collectionView!.registerClass(JkmCollectionCell.self, forCellWithReuseIdentifier: reuseIdentifier)
    }

    override func numberOfSectionsInCollectionView(collectionView: UICollectionView) -> Int {
        return 1
    }

    override func collectionView(collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return itemsCount
    }

    override func collectionView(collectionView: UICollectionView, cellForItemAtIndexPath indexPath: NSIndexPath) -> UICollectionViewCell {
        let cell = collectionView.dequeueReusableCellWithReuseIdentifier(reuseIdentifier, forIndexPath: indexPath)
        
       let cells = cell as! JkmCollectionCell
        cells.ImageView.image = UIImage(named: "new_feature_\(indexPath.item + 1)")
        cells.button.hidden = true
        
        if indexPath.item == 3 {
            cells.button.addTarget(self, action: #selector(clickButton), forControlEvents: .TouchUpInside)
        }
    
        return cell
    }
    
    override func prefersStatusBarHidden() -> Bool {
        return true
    }
        

    private   var  willappearItem  = 0
}

extension JkmNewfeatureViewController{

    override func collectionView(collectionView: UICollectionView, didEndDisplayingCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {

        if  let path = collectionView.indexPathsForVisibleItems().last where path.item == (itemsCount-1) && willappearItem != (itemsCount - 2){
            
                (collectionView.cellForItemAtIndexPath(path) as! JkmCollectionCell).stratAnimation()
        }
    
    
    }
    override func collectionView(collectionView: UICollectionView, willDisplayCell cell: UICollectionViewCell, forItemAtIndexPath indexPath: NSIndexPath) {
       
        willappearItem = indexPath.item
    }
}

//自定义cell
class JkmCollectionCell: UICollectionViewCell {
    
    func stratAnimation(){
        
        button.hidden = false
        button.userInteractionEnabled = false
        button.transform = CGAffineTransformMakeScale(0, 0)
        UIView.animateWithDuration(0.5, delay: 0.0, usingSpringWithDamping: 0.8, initialSpringVelocity: 0, options: [], animations: { 
                self.button.transform = CGAffineTransformIdentity
            }) { (_) in
               self.button.userInteractionEnabled = true
        }
        
    }
    
    lazy var ImageView : UIImageView = {
        let image = UIImageView(frame: UIScreen.mainScreen().bounds)
        return image
    }()
    
    lazy var button : UIButton = {
        let btn  = UIButton(imageName: "", backimageName:"new_feature_button")
        return btn
    }()
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        contentView.addSubview(ImageView)
        contentView.addSubview(button)
        
        button.snp_makeConstraints { (make) in
            
            make.centerX.equalTo(contentView.snp_centerX)
            make.bottom.equalTo(contentView.snp_bottom).offset(-200)
        }
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
